﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Collections;
using System.Data.OleDb;
using System.Data;

namespace FDeal
{
    public class ChiTietDealDAO : DataProvider
    {
        public ChiTietDealDAO()
        { 
        }

        public ChiTietDealDTO[] getDsChiTietDeal()
        {
            connect();
            string query = "SELECT * FROM CHITIETDEAL";
            adapter = new OleDbDataAdapter(query, connection);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset);
            ChiTietDealDTO[] arr;

            DataTable dt = dataset.Tables[0];
            int i, n = dt.Rows.Count;
            arr = new ChiTietDealDTO[n];
            for (i = 0; i < n; i++)
            {
                object lop = GetDataFromDataRow(dt, i);
                arr[i] = (ChiTietDealDTO)lop;
            }
            return arr;
        }



        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            ChiTietDealDTO ctd = new ChiTietDealDTO();
            ctd.Iddeal = dt.Rows[i]["IDDEAL"].ToString();
            ctd.Tieude = dt.Rows[i]["TIEUDE"].ToString();
            ctd.Loaideal = dt.Rows[i]["LOAIDEAL"].ToString();
            ctd.Tomtatdeal = dt.Rows[i]["TOMTATDEAL"].ToString();
            ctd.Hinhanh = dt.Rows[i]["HINHANH"].ToString();
            ctd.Giagoc = float.Parse(dt.Rows[i]["GIAGOC"].ToString());
            ctd.Donvi = dt.Rows[i]["DONVI"].ToString();
            ctd.Giam = int.Parse(dt.Rows[i]["GIAM"].ToString());
            ctd.Giaconlai = float.Parse(dt.Rows[i]["GIACONLAI"].ToString());
            ctd.Songuoimua = int.Parse(dt.Rows[i]["SONGUOIMUA"].ToString());
            ctd.Thoigianconlai = int.Parse(dt.Rows[i]["THOIGIANCONLAI"].ToString());
            ctd.Sodealban = int.Parse(dt.Rows[i]["SODEALBAN"].ToString());
            ctd.Dieukien = dt.Rows[i]["DIEUKIEN"].ToString();
            ctd.Diemnoibat = dt.Rows[i]["DIEMNOIBAT"].ToString();
            ctd.Diachi = dt.Rows[i]["DIACHI"].ToString();
            ctd.Bando = dt.Rows[i]["BANDO"].ToString();
            ctd.Luotxem = int.Parse(dt.Rows[i]["LUOTXEM"].ToString());
            ctd.Ngaydang= DateTime.Parse(dt.Rows[i]["NGAYDANG"].ToString());
            ctd.Nguoidang = dt.Rows[i]["NGUOIDANG"].ToString();
            ctd.Diadiembandeal = dt.Rows[i]["DIADIEMBANDEAL"].ToString();

            return (object)ctd;
        }

        public void insert(ChiTietDealDTO info)
        {
            string insertCommand = "INSERT INTO CHITIETDEAL VALUES ('" +
                info.Iddeal + "','" +
                info.Tieude + "', " +
                info.Loaideal + ",'" +
                info.Tomtatdeal + "','" +
                info.Hinhanh + "'," +
                info.Giagoc + ",'" +
                info.Donvi + "'," +
                info.Giam + "," +
                info.Giaconlai + "," +
                info.Songuoimua + ",'" +
                info.Thoigianconlai + "'," +
                info.Sodealban + ",'" +
                info.Dieukien + "','" +
                info.Diemnoibat + "','" +
                info.Diachi + "','" +
                info.Bando + "'," +
                info.Luotxem + ",'" +
                info.Ngaydang + "','" +
                info.Nguoidang + "','" +

                info.Diadiembandeal + "')";

            executeNonQuery(insertCommand);
        }

        public void update(ChiTietDealDTO info)
        {
            string updateCommand = "UPDATE CHITIETDEAL " +
                                    "SET TIEUDE = '" + info.Tieude + "', " +
                                    " LOAIDEAL = " + info.Loaideal + "," +
                                    " TOMTATDEAL = '" + info.Tomtatdeal + "'," +
                                    " HINHANH = '" + info.Hinhanh + "'," +
                                    " GIAGOC = " + info.Giagoc + "," +
                                    " DONVI = '" + info.Donvi + "'," +
                                    " GIAM = " + info.Giam + "," +
                                    " GIACONLAI = " + info.Giaconlai + "," +
                                    " SONGUOIMUA = " + info.Songuoimua + "," +
                                    " THOIGIANCONLAI = '" + info.Thoigianconlai + "'," +
                                    " SODEALBAN = " + info.Sodealban + "," +
                                    " DIEUKIEN = '" + info.Dieukien + "'," +
                                    " DIEMNOIBAT = '" + info.Diemnoibat + "'," +
                                    " DIACHI = '" + info.Diachi + "'," +
                                    " BANDO = '" + info.Bando + "'," +
                                    " LUOTXEM = " + info.Luotxem + "," +
                                    " NGAYDANG = '" + info.Ngaydang + "'," +
                                    " NGUOIDANG = '" + info.Nguoidang + "'" +

                                    " WHERE IDDEAL  = '" + info.Iddeal + "'";

            executeNonQuery(updateCommand);
        }

        public void delete(string tenbl)
        {
            string deleteCommand = "DELETE FROM CHITIETDEAL WHERE IDDEAL = '" + tenbl + "'";
            executeNonQuery(deleteCommand);
        }

    }
}